<?php
namespace Amoysea\Controller;
use Think\Controller;
class ProductController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        $g=session('company');
        $db_name=$g['db'];
        $this->product = M('product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
    }

    public function index(){
        if(!isset($_GET['p']) || $_GET['p']==''){
            $p=1;
        }else{
            $p=intval($_GET['p']);
        }
        if(I("get.start_time")!='') {
            $where['created_at']= array('gt',I("get.start_time"));
        }
        if(I("get.end_time")!=''){
            $where['created_at'] = array('lt',I("get.end_time"));
        }
        if(I("get.start_time")!='' && I("get.end_time")!=''){
            $where['created_at']= array(array('gt',I("get.start_time")),array('lt',I("get.end_time")));
        }
        if(I("get.name")!=''){
            $where['name']=array('like',"%".I("get.name")."%");
        }
        if(I("get.page_count")=='' || I("get.page_count")<=0){
            $page_count=15;
        }else{
            $page_count=intval(I("get.page_count"));
        }
        if(I("get.sort")!=''){
            $order=I("get.sort")." ".I("get.dec");
        }else{
            $order='id desc';
        }
        if(!isset($where)){
            $where="1=1";
        }
        $list = $this->product->where($where)->order($order)->page($p.','.$page_count)->select();
        $this->assign('list',$list);
        $count      = $this->product->where($where)->count();
        $this->assign('count',$count);
        $Page       = new \Think\Page($count,$page_count);
        $show       = $Page->show();
        $this->assign('page',$show);
        $this->display();
    }
    public function add(){
        $this->assign('time',date("YmdHis",time()).rand(100,999));
        $this->display();
    }
    // 料号、商品规格、币制、产销国
    public function addD($name,$zccode,$unit,$unit_no,$unit_t,$unit_no_t,$unit_t_goods_spec,$category,$sn,$pn,$country,$standard,$currency,$goods_cate){
        if($name==''){
            alert(0,'产品名称不能为空');
        }
        if($zccode==''){
            alert(0,'账册编码不能为空');
        }
        // if($price==''){
            // alert(0,'第一单位价格不能为空');
        // }
        if($unit==''){
            alert(0,'第一单位不能为空');
        }
        if($category==''){
            alert(0,'类目不能为空');
        }
        if($country=='' || $standard=='' || $currency==''){
            alert(0,'产销国/规格/币制都不能为空');
        }
		
		$g=session('company');
        $db_name=$g['db'];
        $this->book = M('book',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
		
		$country_info=explode('-',$country);
		$coin_info=explode('-',$currency);
		
        $data['name']=$name;
        $data['zccode']=$zccode;
		
		$book_info=$this->book->where(array('book_no'=>$zccode))->find();
		$merchant_code=$book_info['merchant_code'];
        // $data['price']=$price;
        $data['unit']=$unit;
        $data['unit_no']=$unit_no;
        // $data['price_t']=$price_t;
        $data['unit_t']=$unit_t;
        $data['unit_no_t']=$unit_no_t;
        $data['category']=$category;
        $massage=session('company');
        $data['operator']=$massage['username'];
		$data['sn']=$sn;
		$sn_length=strlen($sn);
		// if($sn_length<20){
			// $sn_new=$sn;
			// for($i=0;$i<20-$sn_length;$i++){
				// $sn_new=$sn_new.'0';
			// }
		// }else{
			// $sn_new=substr($sn,-20,20);
		// }
		$sn_new=$sn;
		$goods_no=$merchant_code.$sn_new;
        $data['goods_no']=$goods_no;//商家编码（10位）+ 条形码（不足20位数字的在后面补0，超过20位的，截取后面的20位字符）
		//商家编码定义：公司--C+海关十位编码或者组织机构代码。如果有海关十位编码的优先考虑海关十位编码，没有海关十位编码的再考虑组织机构代码
        //注：组织机构代码不足十位的在后边补0
        //个人--P+手机号码。截取手机号码后十位
		$data['country_code']=$country_info[0];
        $data['country']=$country_info[1];
        $data['standard']=$standard;
		$data['unit_t_goods_spec']=$unit_t_goods_spec;
		if(session('company.warehouse_cate')==1){
			$data['pn']=$goods_no;//默认为商品编码
		}else{
			$data['pn']=$pn;
		}

		$data['coin_code']=$coin_info[0];
        $data['currency']=$coin_info[1];
		$data['goods_cate']=$goods_cate;
		
        if($this->product->where(array('name'=>$name))->count()>0){
            alert(0,'产品名称不能重复');
        }
        $result=$this->product->add($data);
        if($result){
            alert(1,'添加成功');
        }else{
            alert(0,'添加失败');
        }
    }
	/* public function addD($name,$zccode,$price,$count,$unit,$unit_no,$category,$goods_no,$country,$standard,$currency){
        if($name==''){
            alert(0,'产品名称不能为空');
        }
        if($zccode==''){
            alert(0,'账册编码不能为空');
        }
        if($price==''){
            alert(0,'第一单位价格不能为空');
        }
        if($count==''){
            alert(0,'第一单位数量不能为空');
        }
        if($unit==''){
            alert(0,'第一单位不能为空');
        }
        if($category==''){
            alert(0,'类目不能为空');
        }
        if($country=='' || $standard=='' ||  $currency==''){
            alert(0,'产销国/规格/料号都不能为空');
        }
		$country_info=explode('-',$country);
		$coin_info=explode('-',$currency);
		
        $data['name']=$name;
        $data['zccode']=$zccode;
        $data['price']=$price;
        $data['unit']=$unit;
        $data['unit_no']=$unit_no;
        $data['count']=$count;
        $data['category']=$category;
        $massage=session('company');
        $data['operator']=$massage['username'];
        $data['goods_no']=$goods_no;
		$data['country_code']=$country_info[0];
        $data['country']=$country_info[1];
        $data['standard']=$standard;
		$data['pn']=$goods_no;
        
		$data['coin_code']=$coin_info[0];
        $data['currency']=$coin_info[1];
		
        if($this->product->where(array('name'=>$name))->count()>0){
            alert(0,'产品名称不能重复');
        }
        $result=$this->product->add($data);
        if($result){
            alert(1,'添加成功');
        }else{
            alert(0,'添加失败');
        }
    } */
    public function push($id,$warehouse_id){
        $g=session('company');
        $db_name=$g['db'];
        $user_id=$g['company_id'];
        $f=C('DB_PREFIX_T');
        $this->p = M('product',$f, C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $list=$this->p->where("id='".$id."'")->find();
        $list['erp_code']=$g['company_id'];
        //////如果已经推送则不能再次推送
        if($list['examined']>0){
            alert(0,'请不要重复推送');
        }
        unset($list['examined']);
        $g_id=M("user_warehouse")->where(array('user_id'=>$user_id))->getField('warehouse_group_id');
        $group_id=explode(',',$g_id);
        if(!in_array($warehouse_id,$group_id)){
            alert(0,'操作有误');
        }
        $d=M("warehouse")->join("__INTERFACES__ on __INTERFACES__.warehouse_id=__WAREHOUSE__.id")->where(C('DB_PREFIX')."warehouse.id='".$warehouse_id."'")->getField('vars,url,warehouse_code');
        $url=$d['var_4']['url'];
        $data=array(
            'product_id'=>$id,
            'erp_code'=>$g['company_id'],
            'warehouse_code'=>$d['var_4']['warehouse_code'],
            'list'=>$list,
        );
        $true=push($url,$data);//推送过去
        $true=json_decode($true,'true');
        if($true['status']==1){
            if($this->product->where("id='".$id."'")->setField('examined',1)){
                alert(1,'推送成功');
            }else{
                alert(0,'推送成功，状态更新失败');
            }
        }elseif($true['status']==2){
            alert(0,'对应的账册信息还没有推送');
        }else{
            alert(0,'推送失败');
        }
    }
    public function getWarehouse(){
        $g=session('company');
        $user_id=$g['company_id'];
        $group_id=M("user_warehouse")->where(array('user_id'=>$user_id))->getField('warehouse_group_id');
        $group_id=explode(',',$group_id);
        $where['id']=array('in',$group_id);
        $result=M("warehouse")->where($where)->getField('id,name',true);
        if($result){
            alert(1,$result);
        }else{
            alert(0,'暂无仓库信息');
        }
    }
    public function getBook(){
        $g=session('company');
        $user_id=$g['company_id'];
        $where['examined']=2;
        $result=M('book',C("DB_PREFIX_T"), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $g['db'])->where($where)->getField('book_no',true);
        if($result){
            alert(1,$result);
        }else{
            alert(0,'暂无账册信息');
        }
    }
    public function edit($id){
        if($id==''){
            echo "错误";
        }
        $this->result=$this->product->find($id);
        $this->display();
    }
    public function editD($id,$name,$zccode,$unit,$unit_no,$unit_t_goods_spec,$unit_t,$unit_no_t,$category,$sn,$pn,$goods_no,$country,$standard,$currency,$goods_cate){
		// dump(I());
		// die;
        if($name==''){
            alert(0,'产品名称不能为空');
        }
		if($sn==''){
            alert(0,'条形码不能为空');
        }
        if($zccode==''){
            alert(0,'账册编码不能为空');
        }
        
        if($unit==''){
            alert(0,'第一单位不能为空');
        }
        if($category==''){
            alert(0,'类目不能为空');
        }
        if($country=='' || $standard=='' || $currency==''){
            alert(0,'产销国/规格/币制都不能为空');
        }
        $where['sn']=$sn;
        $where['id']=array('neq',$id);
        if($this->product->where($where)->count()>0){
            alert(0,'条形码不能重复');
        }
		
		$g=session('company');
        $db_name=$g['db'];
        $this->book = M('book',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
		
		$country_info=explode('-',$country);
		$coin_info=explode('-',$currency);
        $data['name']=$name;
        $data['zccode']=$zccode;

		$book_info=$this->book->where(array('book_no'=>$zccode))->find();
		$merchant_code=$book_info['merchant_code'];
		
        $data['unit']=$unit;
        $data['unit_no']=$unit_no;
        $data['unit_t']=$unit_t;
        $data['unit_no_t']=$unit_no_t;
        $data['category']=$category;
		$data['sn']=$sn;
		$sn_length=strlen($sn);

		if($sn_length<20){
			$sn_new=$sn;
			for($i=0;$i<(20-$sn_length);$i++){
				$sn_new=$sn_new.'0';
			}
		}else{
			$sn_new=substr($sn,-20,20);
		}
		// $goods_no=$merchant_code.$sn_new;
        // $data['goods_no']=$goods_no;//商家编码（10位）+ 条形码（不足20位数字的在后面补0，超过20位的，截取后面的20位字符）
		//商家编码定义：公司--C+海关十位编码或者组织机构代码。如果有海关十位编码的优先考虑海关十位编码，没有海关十位编码的再考虑组织机构代码
        //注：组织机构代码不足十位的在后边补0
        //个人--P+手机号码。截取手机号码后十位
		$data['goods_no']=$goods_no;
		$data['country_code']=$country_info[0];
        $data['country']=$country_info[1];
        $data['standard']=$standard;
		$data['unit_t_goods_spec']=$unit_t_goods_spec;
		if(session('company.warehouse_cate')==1){
			$data['pn']=$goods_no;//默认为商品编码
		}else{
			$data['pn']=$pn;
		}
        
		$data['coin_code']=$coin_info[0];
        $data['currency']=$coin_info[1];
		$data['goods_cate']=$goods_cate;
        $massage=session('company');
        $data['operator']=$massage['username'];
        $result=$this->product->where("id='".$id."'")->save($data);
        if($result!==false){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
	/* public function editD($id,$name,$zccode,$price,$count,$unit,$unit_no,$category,$goods_no,$country,$standard,$currency){
        if($name==''){
            alert(0,'产品名称不能为空');
        }
        if($zccode==''){
            alert(0,'账册编码不能为空');
        }
        if($price==''){
            alert(0,'第一单位价格不能为空');
        }
        if($count==''){
            alert(0,'第一单位数量不能为空');
        }
        if($unit==''){
            alert(0,'第一单位不能为空');
        }
        if($category==''){
            alert(0,'类目不能为空');
        }
        if($country=='' || $standard=='' ||  $currency==''){
            alert(0,'产销国/规格/料号都不能为空');
        }
        $where['name']=$name;
        $where['id']=array('neq',$id);
        if($this->product->where($where)->count()>0){
            alert(0,'产品名称不能重复');
        }
		$country_info=explode('-',$country);
		$coin_info=explode('-',$currency);
        $data['name']=$name;
        $data['zccode']=$zccode;
        $data['price']=$price;
        $data['unit']=$unit;
        $data['unit_no']=$unit_no;
        $data['count']=$count;
        $data['category']=$category;
        $data['goods_no']=$goods_no;
		$data['country_code']=$country_info[0];
        $data['country']=$country_info[1];
        $data['standard']=$standard;
        $data['pn']=$goods_no;
		$data['coin_code']=$coin_info[0];
        $data['currency']=$coin_info[1];
        $massage=session('company');
        $data['operator']=$massage['username'];
        $result=$this->product->where("id='".$id."'")->save($data);
        if($result){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    } */
    public function del($id){
        $g=session('company');
        $db_name=$g['db'];
        $this->p = M('list_product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        if($id==''){
            alert(0,'操作有误');
        }
        ///////$id=1@2@3
        $group_id=explode('@',$id);
        $where['product_id']=array('IN',$group_id);
        $count=$this->p->where($where)->count();
        if($count>0){
            alert(0,'部分产品已经制单，不能删除');
        }
        $id=implode(',',$group_id);
        $result=$this->product->delete($id);
        if($result){
            alert(1,'成功删除了'.$result.'条数据，共'.count($group_id).'条');
        }else{
            alert(0,'删除失败');
        }
    }
    public function hscode(){
        $hscode = M('hscode');
        if(I("get.name")!==null && I("get.name")!==''){
            $name_code=trim(I("get.name"));
            if(is_numeric($name_code) && strlen($name_code)>5){
                $where['hscode']=array('like',"%".$name_code."%");
            }else{
                $where['name']=array('like',"%".$name_code."%");
                if($hscode->where($where)->count()==0){//如果么有搜索结果，则分词
                    //将多个空格换成一个空格
                    $name_code=preg_replace("/\s(?=\s)/","\\1",$name_code);
                    $group_word=explode(' ',$name_code);
                    if(count($group_word)==1){
                        $group_word=explode(',',$name_code);
                    }
                    if(count($group_word)==1){
                        $group_word=explode('，',$name_code);
                    }
                    $g_count=count($group_word);
                    $n_where=array();
                    for($i=0;$i++;$i<$g_count){
                        $w=array();$where=array();
                        for($j=0;$j<$i;$j++){
                            $w[]=array('like',"%".$group_word[$j]."%");
                        }
                        $where['name']=$w;
                        if($hscode->where($where)->count()==0){
                            break;
                        }else{
                            $n_where=$where;
                            unset($where,$w);
                        }
                    }
                    if(count($n_where)==0){
                        $where="1=2";//没有记录
                    }else{
                        $where=$n_where;
                    }
                }
            }
        }else{
            $where="1=1";
        }
        $count      = $hscode->where($where)->count();
        $Page       = new \Think\Page($count,15);
        $show       = $Page->show();
        $list = $hscode->where($where)->limit($Page->firstRow.','.$Page->listRows)->order("id")->select();
        $this->assign('list',$list);
        $this->assign('page',$show);
        $this->assign('count',$count);
        $this->display();
    }
}